﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MensUnoShop.Models
{
    public abstract class Market
    {
        protected Guid _mkguid;           //賣場ID
        protected long _tagid;            //分類ID
        protected string _title;          //標題
        protected int _showprice;         //市價
        protected int _price;             //賣場金額
        protected bool _status;           //是否上架
        protected string _img;
        protected long _colortagid;
        protected long _grouptagid;
        protected string _colortitle;
        protected string _grouptitle;
        protected string _sn;
        protected Config.MarketType _type;

        protected List<Product> _plist;   //產品

        protected DateTime _sdate;        //開賣時間
        protected DateTime _edate;        //停賣時間
        protected DateTime _datein;
        protected DateTime _lastmodify;

        public Guid MKGUID { set { _mkguid = value; } get { return _mkguid; } }
        public string SN { set { _sn = value; } get { return _sn; } }
        public long TAGID { set { _tagid = value; } get { return _tagid; } }
        public string TITLE { set { _title = value; } get { return _title; } }
        public long COLORTAGID { set { _colortagid = value; } get { return _colortagid; } }
        public string COLORTITLE { set { _colortitle = value; } get { return _colortitle; } }
        public long GROUPTAGID { set { _grouptagid = value; } get { return _grouptagid; } }
        public string GROUPTITLE { set { _grouptitle = value; } get { return _grouptitle; } }
        public string IMG { set { _img = value; } get { return _img; } }
        public int PRICE { set { _price = value; } get { return _price; } }
        public int SHOPPRICE { set { _showprice = value; } get { return _showprice; } }
        public bool STATUS { set { _status = value; } get { return _status; } }
        public List<Product> PRODUCTLIST { set { _plist = value; } get { return _plist; } }
        public DateTime SDATE { set { _sdate = value; } get { return _sdate; } }
        public DateTime EDATE { set { _edate = value; } get { return _edate; } }
        public DateTime DATEIN { set { _datein = value; } get { return _datein; } }
        public DateTime LASTMODIFY { set { _lastmodify = value; } get { return _lastmodify; } }
        public Config.MarketType TYPE { set { _type = value; } get { return _type; } }
        /// <summary>
        /// 組合賣場URI
        /// </summary>
        /// <returns></returns>
        public string GetMarketUri()
        {
            string url = string.Empty;
            url = "/market/n/" + _sn + "/";
            if (_type == Config.MarketType.Combo)
                url = "/market/combo/" + _sn + "/";
            if (_plist != null)
                url += _plist.First().TITLETAGID;
            if(_tagid!=0)
                url+= "/" + _tagid;
            if (_type == Config.MarketType.Mangazine)
                url = "/market/maga/" + _sn + "/";
            return url;
        }

        //初始化購物賣場
        protected OrderMarket initialOrderMarket(Guid productid, int amount, Guid specid)
        {
            OrderMarket om = new OrderMarket();
            om.MOKGUID = Guid.NewGuid();
            om.MKGUID = _mkguid;
            om.TITLE =_grouptitle=="N/A"?_title:"【"+_grouptitle+"系列】"+ _title;
            om.SPECID = specid;
            om.TYPE = _type;
            if (amount > 0)
            {
                var pd = _plist.SingleOrDefault(p => p.PDGUID == productid);
                if (pd != null)
                {
                    om.IMG = pd.PICBIG;
                    pd.GetSpecification();
                    var sp = pd.SPEC.SingleOrDefault(spec => spec.SID == specid);
                    if (sp != null)
                    {

                        om.AMOUNT = amount;
                        om.SINGLEPRICE = _price;
                        om.SN = _sn + "-" + pd.TITLETAGID + "-" + sp.TITLE;
                        om.COLOR = pd.TITLE;
                    }
                }
            }
            if (_type == Config.MarketType.Combo)
                om.COMBOID = (int)_tagid;
            return om;
        }
    }
}
